#! /bin/sh

if [ $# -ne 6 ]; then
  echo "Usage: mbxmlutils-xsd2html <InXsdFile> <InDoxygenXmlDir> <FigureDir>"
  echo "                           <ProjectName> <DocygenPrefix> <IncludeDoxygen>"
  echo "  InXsdFile: Input xsd file which should be converted to html"
  echo "  InDoxygenXmlDir: Directory of the Doxygen XML output of the"
  echo "                   corrospondending documentation"
  echo "  FigureDir: Colon separaded directory to search for figures (*.eps, *.png)"
  echo "  ProjectName: The name of the Project (e.g. 'OpenMBV')"
  echo "  DoxygenPrefix: Prefix of doxygen names (e.g. 'OpenMBV::')"
  echo "  IncludeDoxygen: If true, include the doxygen part in the documentation"
  exit
fi

FILE=$1 #"/home/markus/project/MBSim/openmbv/schema/openmbv.xsd"
DOXYGENDOCDIR=$2 #"/home/markus/project/MBSim/openmbv/doc_/xml"
FIGUREDIR=$3
PROJECT=$4 #"OpenMBV"
DOXYGENCLASSPREFIX=$5 #"OpenMBV::"
INCLUDEDOXYGEN=$6



DOXYGENFUNCTIONPREFIX1="set"
DOXYGENFUNCTIONPREFIX2="add"
BASENAME=$(basename $FILE .xsd)
prefix=@prefix@

if [ $INCLUDEDOXYGEN = "true" ]; then
  echo "Combine Doxygen XML documentation"
  echo "  In file: $DOXYGENDOCDIR/index.xml"
  echo "  XSL file: $DOXYGENDOCDIR/combine.xslt"
  echo "  Out file: .$PROJECT.doxygen.xml"
  @XSLTPROC@ --novalid --output .$PROJECT.doxygen.xml $DOXYGENDOCDIR/combine.xslt $DOXYGENDOCDIR/index.xml || exit 1
  echo "DONE"
  
  echo "Add Doxygen class and element function documentation to XML Schema"
  echo "  In file: $FILE"
  echo "  XSL file: @datadir@/@PACKAGE@/xsl/addDoxygenToXsd.xsl"
  echo "  Out file: $BASENAME.doxygen.xsd"
  @XSLTPROC@ --novalid --output $BASENAME.doxygen.xsd \
    --param DOXYGENDOC "'$(pwd)/.$PROJECT.doxygen.xml'" \
    --param DOXYGENCLASSPREFIX "'$DOXYGENCLASSPREFIX'" \
    --param DOXYGENFUNCTIONPREFIX1 "'$DOXYGENFUNCTIONPREFIX1'" \
    --param DOXYGENFUNCTIONPREFIX2 "'$DOXYGENFUNCTIONPREFIX2'" \
    @datadir@/@PACKAGE@/xsl/addDoxygenToXsd.xsl $FILE || exit 1
  echo "DONE"
else
  echo "Do not add Doxygen class docu"
  cp $FILE $BASENAME.doxygen.xsd
fi




HTMLDIR=$(sed -rne '/ targetNamespace *=/s|^.*targetNamespace *= *"([^"]+)".*$|\1|p' $BASENAME.doxygen.xsd | sed -re 's|\.|_|g;s|:|_|g;s|/|_|g')
echo "Generate HTML documentation"
echo "  In file: $BASENAME.doxygen.xsd"
echo "  XSL file: @datadir@/@PACKAGE@/xsl/xsdToHtml.xsl"
echo "  Out file: $HTMLDIR/index.xhtml"
rm -rf $HTMLDIR
@XSLTPROC@ --novalid --output $HTMLDIR/index.xhtml \
  --param PROJECT "'$PROJECT'" \
  --param INCLUDEDOXYGEN "'$INCLUDEDOXYGEN'" \
  @datadir@/@PACKAGE@/xsl/xsdToHtml.xsl $BASENAME.doxygen.xsd || exit 1
echo "DONE"

echo "Validate generated HTML documentation"
echo "  In file: $HTML.xhtml"
echo "  XSD file: @datadir@/@PACKAGE@/schema/xhtml1-transitional.xsd"
@XMLLINT@ --noout --schema @datadir@/@PACKAGE@/schema/xhtml1-transitional.xsd $HTMLDIR/index.xhtml || exit 1

echo "Copy *.png"
for D in $(echo $FIGUREDIR | tr ':' ' '); do
  ls $D/*.png >& /dev/null && cp -f $D/*.png $HTMLDIR
done

echo "Export equations"
echo "  In file: $BASENAME.doxygen.xsd"
echo "  XSL file: @datadir@/@PACKAGE@/xsl/xsdToEquation.xsl"
echo "  Out file: equation.txt"
@XSLTPROC@ --novalid --output equation.txt @datadir@/@PACKAGE@/xsl/xsdToEquation.xsl $HTMLDIR/index.xhtml || exit 1

# Generating equation
while grep -E "^MBXMLUTILS_BEGINEQN$" equation.txt >& /dev/null; do
  B=$(sed -rne "/^MBXMLUTILS_BEGINEQN$/=;/^MBXMLUTILS_BEGINEQN$/q" equation.txt)
  E=$(sed -rne "/^MBXMLUTILS_ENDEQN$/=;/^MBXMLUTILS_ENDEQN$/q" equation.txt)
  FILE=$(sed -rne "$[$B+1]p" equation.txt)
  echo "Generating equation in $FILE"
  cat << EOF > equation.tex
\documentclass{report}
\usepackage{graphicx,psfrag,amsmath,amssymb,amsbsy,color}
\setlength{\parindent}{0cm}
\setlength{\textwidth}{100cm}
\setlength{\textheight}{100cm}
\begin{document}
\pagestyle{empty}
EOF
  sed -rne "$[$B+2],$[$E-1]p" equation.txt >> equation.tex
  echo '\end{document}' >> equation.tex
  latex -help >& /dev/null && latex equation.tex || echo "Skip generation of png-equations (LaTeX not found)"
  latex -help >& /dev/null && dvips -E -o equation.eps equation.dvi || echo ""
  latex -help >& /dev/null && gs -q -dBATCH -dNOPAUSE -dEPSCrop -r144 -sDEVICE=pngalpha -sOutputFile=$FILE equation.eps || echo ""
  mv $FILE $HTMLDIR/$FILE
  sed -rne "$[$E+1],\$p" equation.txt >> equation.txt.new
  mv equation.txt.new equation.txt
done
rm -f equation.*





echo "Generate LaTeX documentation"
echo "  In file: $BASENAME.doxygen.xsd"
echo "  XSL file: @datadir@/@PACKAGE@/xsl/xsdToTex.xsl"
echo "  Out file: $BASENAME/$BASENAME.tex"
@XSLTPROC@ --novalid --output $BASENAME/$BASENAME.tex \
  --param PROJECT "'$PROJECT'" \
  --param INCLUDEDOXYGEN "'$INCLUDEDOXYGEN'" \
  @datadir@/@PACKAGE@/xsl/xsdToTex.xsl $BASENAME.doxygen.xsd || exit 1
echo "DONE"

echo "Copy *.eps"
for D in $(echo $FIGUREDIR | tr ':' ' '); do
  ls $D/*.eps >& /dev/null && cp -f $D/*.eps $BASENAME
done

echo "Generate LaTeX-PS documentation"
echo "  In file: $BASENAME/$BASENAME.tex"
echo "  Out file: $BASENAME/$BASENAME.ps"
OLDDIR=$(pwd)
cd $BASENAME
latex -help >& /dev/null && latex -interaction errorstopmode $BASENAME.tex || echo "Skip running LaTeX"
latex -help >& /dev/null && latex $BASENAME.tex >& /dev/null || echo "Skip running LaTeX"
latex -help >& /dev/null && latex $BASENAME.tex >& /dev/null || echo "Skip running LaTeX"
latex -help >& /dev/null && dvips -o $BASENAME.ps $BASENAME.dvi || echo "Skip running LaTeX"
cd $OLDDIR
